package com.omp.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.Struts2Utils;

import com.omp.common.action.Struts2ActionSupport;
import com.omp.common.util.DateUtil;
import com.omp.entity.Product;
import com.omp.entity.ProductCatalog;
import com.omp.service.IndustryService;
import com.omp.service.ProductCatalogService;
import com.omp.service.ProductService;

@SuppressWarnings("serial")
@Namespace("/product")
@Results({
	@Result(name = Struts2ActionSupport.RELOAD, type = "redirect", location = "product.action"),
	@Result(name = "product_list", type = "redirect", location = "product!productList.action", params = {"industryId", "${industryId}"})
})
public class ProductAction extends Struts2ActionSupport<Product> {
	
	private Integer id; //产品ID
	private Product product = new Product();
	private Page<Product> page = new Page<Product>(2);
	private List<Integer> ids;
	private Integer industryId; //行业ID
	private Boolean findNoneIndustry = false;
	private String beginDate;
	private String endDate;
	
	@Autowired
	private ProductService productService;
	@Autowired
	private IndustryService	industryService;
	@Autowired
	private ProductCatalogService productCatalogService;

	@Override
	public String list() throws Exception {
		page = productService.findByPage(page, product);
		return "list";
	}
	
	public String input() {
		return "input";
	}
	
	public String save() {
		productService.save(product);
		return RELOAD;
	}
	
	public String update() {
		productService.update(product);
		return RELOAD;
	}
	
	public String delete() {
		productService.delete(id);
		return RELOAD;
	}
	
	public String batchDelete() {
		productService.batchDelete(ids);
		return RELOAD;
	}
	
	public void nameExists() {
		Struts2Utils.renderJson(productService.nameExists(product.getName()));
	}
	
	public String catalog() {
		request.setAttribute("industryList", industryService.findAll());
		return "catalog";
	}
	
	public String productList() {
		List<Product> productList = productService.findByIndustryId(industryId, product, findNoneIndustry);
		if (productList != null && productList.size() > 0) {
			for (Product product : productList) {
				ProductCatalog productCatalog = productCatalogService.findById(industryId, product.getId());
				if (productCatalog != null) {
					product.setBeginDateShow(productCatalog.getBeginDate());
					product.setIndustryName("insdustryName");
				}
			}
		}
		request.setAttribute("productList", productList);
		return "catalog_list";
	}
	
	public String saveValidDate() {
		String beginDateMode = request.getParameter("beginDateMode");
		if ("now".equals(beginDateMode)) {
			beginDate = DateUtil.getCurrentTimeUsePattern("yyyy-MM-dd HH:mm:00");
		}
		productService.saveValidDate(industryId, ids, beginDate, endDate);
		return "product_list";
	}
	
	public String configDate() {
		if (ids != null && ids.size() == 1) {
			ProductCatalog productCatalog = productCatalogService.findById(industryId, ids.get(0));
			if (productCatalog != null) {
				beginDate = productCatalog.getBeginDate();
				endDate = productCatalog.getEndDate();
			}
		}
		return "config_date";
	}
	
	public String saveProductCatalog() {
		productService.saveProductCatalog(industryId, ids);
		return "product_list";
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			product = productService.findById(id);
		}
	}

	@Override
	public Product getModel() {
		return product;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Page<Product> getPage() {
		return page;
	}

	public void setPage(Page<Product> page) {
		this.page = page;
	}

	public List<Integer> getIds() {
		return ids;
	}

	public void setIds(List<Integer> ids) {
		this.ids = ids;
	}

	public Integer getIndustryId() {
		return industryId;
	}

	public void setIndustryId(Integer industryId) {
		this.industryId = industryId;
	}

	public Boolean getFindNoneIndustry() {
		return findNoneIndustry;
	}

	public void setFindNoneIndustry(Boolean findNoneIndustry) {
		this.findNoneIndustry = findNoneIndustry;
	}

	public String getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

}
